package com.bringspring.oauth.method.dingding.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import java.util.Collection;

public class DingTalkAuthenticationToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = 562L;
    private final String code;
    private final Object principal;
    private Object credentials;

    public DingTalkAuthenticationToken(String code, String principal, String credentials) {
        super((Collection) null);
        this.code = code;
        this.principal = principal;
        this.credentials = credentials;
        this.setAuthenticated(false);
    }

    public DingTalkAuthenticationToken(String code, Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.code = code;
        this.principal = principal;
        this.credentials = credentials;
        this.setAuthenticated(true);

    }

    public String getCode() {
        return code;
    }

    public Object getCredentials() {
        return this.credentials;
    }

    public Object getPrincipal() {
        return this.principal;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        Assert.isTrue(!isAuthenticated, "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        super.setAuthenticated(false);
    }

    public void eraseCredentials() {
        super.eraseCredentials();
        this.credentials = null;
    }
}
